require 'simple-json.rb'
require 'net/http'

class Json
	def Json::get_json(url, path, port=80)
			htmlresponse = ""
			Net::HTTP.version_1_2
			Net::HTTP.start(url, port) {|http|
				htmlresponse = http.get(path)
			}
	
			parser = JsonParser.new
			ruby_obj = parser.parse(htmlresponse.body)

		return (ruby_obj)
	end

	def Json::get_json_from_file(filename)
		begin
	#		printf("Json:: get_json_from_file: [%s]\n", filename)
			f = open(filename)
			data = f.gets
	
			parser = JsonParser.new
			ruby_obj = parser.parse(data)
		rescue
			puts "ERROR: Json::get_json_from_file"
			sleep (1)
			retry
		end

		return (ruby_obj)
	end

	def Json::get_jsonobj(url, path, port=80)
#		printf("Json:: get_json_obj: [%s][%s][%s]\n", url, path, port)

		htmlresponse = ""
		Net::HTTP.version_1_2
		Net::HTTP.start(url, port) {|http|
			htmlresponse = http.get(path)
		}

		return (htmlresponse.body)
	end

	def Json::put_json(url, path, port, data)
		builder = JsonBuilder.new
		json_string = builder.build(data)

		printf("Json.rb: put_json [%s][%s][%s]\n",
			url, path, port) # DEBUG
		p json_string # DEBUG
		Net::HTTP.version_1_2
		http = Net::HTTP.new(url, port)
		response = http.post(path, json_string)
		p response.body # DEBUG

		parser = JsonParser.new
		ruby_obj = parser.parse(response.body)

		return (ruby_obj)
	end

end
